package com.icss.service.impl;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;

import com.icss.common.service.BaseServiceImpl;
import com.icss.common.util.ConstatFinalUtil;
import com.icss.common.util.PageInfoUtil;
import com.icss.common.vo.ApiResponse;
import com.icss.common.vo.ApiResponseEnum;
import com.icss.common.vo.JSONEnum;
import com.icss.dao.IMakeupCateDao;
import com.icss.dao.IMakeupProductDao;
import com.icss.dao.impl.MakeupCategoryDaoImpl;
import com.icss.dao.impl.MakeupProductDaoImpl;
import com.icss.service.IProductDbService;
import com.icss.vo.MakeupCategory;
import com.icss.vo.MakeupProduct;

/**
 * Service实现类(商品代码)
 * 
 * @author 陈岩
 */
public class ProductDbServiceImpl extends BaseServiceImpl implements IProductDbService
{
	private IMakeupProductDao productDao = new MakeupProductDaoImpl();
	@Resource
	private IMakeupCateDao cateDao = new MakeupCategoryDaoImpl();
	
	@Override
	public ApiResponse<Object> saveOneProductService(MakeupProduct product)
	{
		ApiResponse<Object> apiResponse = new ApiResponse<Object>();

		product.setCreateTime(new Date());
		product.setUpdateTime(new Date());
		int res = this.productDao.save(product);
		if (res > 0)
		{
			Map<String, Object> dataMap = new HashMap<String, Object>();
			dataMap.put(JSONEnum.EFFECT.getName(), res);
			dataMap.put(JSONEnum.ID.getName(), product.getId());
			dataMap.put(ApiResponseEnum.NAME_ONE.getName(), product);
			apiResponse.setDataOne(dataMap);

			apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
		} else
		{
			apiResponse.setCode(ApiResponseEnum.STATUS_FAILED.getStatus());
		}
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
		return apiResponse;
	}

	@Override
	public ApiResponse<Object> updateOneProductService(MakeupProduct product)
	{
		ApiResponse<Object> apiResponse = new ApiResponse<Object>();
		product.setUpdateTime(new Date());
		int res = this.productDao.update(product);
		if (res > 0)
		{
			Map<String, Object> dataMap = new HashMap<String, Object>();
			dataMap.put(JSONEnum.EFFECT.getName(), res);
			dataMap.put(JSONEnum.ID.getName(), product.getId());
			dataMap.put(ApiResponseEnum.NAME_ONE.getName(), product);
			apiResponse.setDataOne(dataMap);

			apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
		} else
		{
			apiResponse.setCode(ApiResponseEnum.STATUS_FAILED.getStatus());
		}
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
		return apiResponse;
	}
	
	@Override
	public ApiResponse<Object> updateOneProductService(MakeupProduct product, Map<String, Object> paramsMap)
	{
		/* 先更新用户的信息 */
		ApiResponse<Object> apiResponse = new ApiResponse<Object>();
		try
		{
			String fileName = paramsMap.get("fileName") + "";
			InputStream is = (InputStream) paramsMap.get("is") ;
			
			/* 上传文件的相对路径   /uploadfile/  */
			String uploadFile = ConstatFinalUtil.CONFIG_JSON.getString("website.uploadFile");
			/* 存储的是网址的绝对路径 */ 
			String souPath = product.getPic();
			/* 真实路径  D:/项目空间/usersCenter/usersCenter-web/usersCenter-web-head/src/main/webapp/ */
			String truePath = ConstatFinalUtil.CONFIG_JSON.getString("website.truePath");
			/* 网站的相对路径 */
			String tarRelaPath = uploadFile + "product/" + this.dateFormatUtil.dateStr(new Date()) + "/" + UUID.randomUUID().toString() + 
					fileName.substring(fileName.lastIndexOf("."), fileName.length());
			File tarTrueFile = new File(truePath + tarRelaPath.substring(1));
			
			/* 创建父目录 */
			if(!tarTrueFile.getParentFile().exists())
			{
				tarTrueFile.getParentFile().mkdirs();
			}
			/* 设置路径 */
			product.setPic(tarRelaPath);
			FileOutputStream fos = new FileOutputStream(tarTrueFile);
			/* 复制文件 */
			boolean flag = this.fileUtil.copyFile(is, fos);
			if(flag)
			{
				if(souPath != null && !"".equalsIgnoreCase(souPath))
				{
					souPath=truePath+souPath;
					/* 相对路径 */
					File souFile = new File(souPath);
					if(!souFile.delete())
					{
						ConstatFinalUtil.SYS_LOGGER.error("删除原始文件失败:原始文件路径:{}",souFile.getAbsolutePath());
					}
				}
			}
			/* 更新用户信息 */
			apiResponse = this.updateOneProductService(product);
		} catch (Exception e)
		{
			ConstatFinalUtil.SYS_LOGGER.error("上传头像失败了,id:{}",product.getId() , e);
		}
		return apiResponse;
	}

	@Override
	public ApiResponse<Object> deleteOneProductService(Map<String, Object> condMap)
	{
		ApiResponse<Object> apiResponse = new ApiResponse<Object>();
		int res = this.productDao.delete(condMap);
		if (res > 0)
		{
			Map<String, Object> dataMap = new HashMap<String, Object>();
			dataMap.put(JSONEnum.EFFECT.getName(), res);
			apiResponse.setDataOne(dataMap);

			apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
		} else
		{
			apiResponse.setCode(ApiResponseEnum.STATUS_FAILED.getStatus());
		}
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
		return apiResponse;
	}

	@Override
	public ApiResponse<MakeupProduct> findCondListProductService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap)
	{
		ApiResponse<MakeupProduct> apiResponse = new ApiResponse<MakeupProduct>();
		apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
		List<MakeupProduct> demoProductList = Collections.EMPTY_LIST;

		Map<String, List<MakeupProduct>> dataList = new HashMap<String, List<MakeupProduct>>();

		demoProductList = this.productDao.findCondList(pageInfoUtil, condMap);
		
		if("true".equalsIgnoreCase(condMap.get("extend") + ""))
		{
			/* 查询关联关系 */
			Map<String, Object> extMap = new HashMap<String, Object>();
			
			for (Iterator iterator = demoProductList.iterator(); iterator.hasNext();)
			{
				MakeupProduct product = (MakeupProduct) iterator.next();
				
				extMap.clear();
				extMap.put("id", product.getCategoryId());
				ApiResponse<MakeupCategory> cateApiResponse = this.findOneCateService(extMap);
				MakeupCategory category = cateApiResponse.getDataOneJava();
				product.setCategory(category);
				
			}
		}
		
		/* 将结果放到JSON中 */
		dataList.put(ApiResponseEnum.NAME_LIST.getName(), demoProductList);

		apiResponse.setPageInfoUtil(pageInfoUtil);
		apiResponse.setDataListJava(demoProductList);
		apiResponse.setDataList(dataList);
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
		return apiResponse;
	}

	@Override
	public ApiResponse<MakeupProduct> findOneProductService(Map<String, Object> condMap)
	{
		ApiResponse<MakeupProduct> apiResponse = new ApiResponse<MakeupProduct>();

		Map<String, MakeupProduct> data = new HashMap<String, MakeupProduct>();
		MakeupProduct product = this.productDao.findOne(condMap);
		
		if("true".equalsIgnoreCase(condMap.get("extend") + ""))
		{
			/* 查询关联关系 */
			Map<String, Object> extMap = new HashMap<String, Object>();
			extMap.clear();
			extMap.put("id", product.getCategory());
			ApiResponse<MakeupCategory> cateApiResponse = this.findOneCateService(extMap);
			MakeupCategory category = cateApiResponse.getDataOneJava();
			product.setCategory(category);
		}
		
		data.put(ApiResponseEnum.NAME_ONE.getName(), product);
		apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
		/* 将结果放到JSON中 */
		apiResponse.setDataOne(data);
		apiResponse.setDataOneJava(product);
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
		return apiResponse;
	}
	
	@Override
	public ApiResponse<Object> saveOneCateService(MakeupCategory cate)
	{
		ApiResponse<Object> apiResponse = new ApiResponse<Object>();

		cate.setCreateTime(new Date());
		int res = this.cateDao.save(cate);
		if (res > 0)
		{
			Map<String, Object> dataMap = new HashMap<String, Object>();
			dataMap.put(JSONEnum.EFFECT.getName(), res);
			dataMap.put(JSONEnum.ID.getName(), cate.getId());
			dataMap.put(ApiResponseEnum.NAME_ONE.getName(), cate);
			apiResponse.setDataOne(dataMap);

			apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
		} else
		{
			apiResponse.setCode(ApiResponseEnum.STATUS_FAILED.getStatus());
		}
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
		return apiResponse;
	}

	@Override
	public ApiResponse<Object> updateOneCateService(MakeupCategory cate)
	{
		ApiResponse<Object> apiResponse = new ApiResponse<Object>();
		int res = this.cateDao.update(cate);
		if (res > 0)
		{
			Map<String, Object> dataMap = new HashMap<String, Object>();
			dataMap.put(JSONEnum.EFFECT.getName(), res);
			dataMap.put(JSONEnum.ID.getName(), cate.getId());
			dataMap.put(ApiResponseEnum.NAME_ONE.getName(), cate);
			apiResponse.setDataOne(dataMap);

			apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
		} else
		{
			apiResponse.setCode(ApiResponseEnum.STATUS_FAILED.getStatus());
		}
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
		return apiResponse;
	}

	@Override
	public ApiResponse<Object> deleteOneCateService(Map<String, Object> condMap)
	{
		ApiResponse<Object> apiResponse = new ApiResponse<Object>();
		int res = this.cateDao.delete(condMap);
		if (res > 0)
		{
			Map<String, Object> dataMap = new HashMap<String, Object>();
			dataMap.put(JSONEnum.EFFECT.getName(), res);
			apiResponse.setDataOne(dataMap);

			apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
		} else
		{
			apiResponse.setCode(ApiResponseEnum.STATUS_FAILED.getStatus());
		}
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
		return apiResponse;
	}

	@Override
	public ApiResponse<MakeupCategory> findCondListCateService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap)
	{
		ApiResponse<MakeupCategory> apiResponse = new ApiResponse<MakeupCategory>();
		apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
		List<MakeupCategory> demoCateList = Collections.EMPTY_LIST;

		Map<String, List<MakeupCategory>> dataList = new HashMap<String, List<MakeupCategory>>();
		demoCateList = this.cateDao.findCondList(pageInfoUtil, condMap);
		/* 将结果放到JSON中 */
		dataList.put(ApiResponseEnum.NAME_LIST.getName(), demoCateList);

		apiResponse.setPageInfoUtil(pageInfoUtil);
		apiResponse.setDataListJava(demoCateList);
		apiResponse.setDataList(dataList);
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
		return apiResponse;
	}

	@Override
	public ApiResponse<MakeupCategory> findOneCateService(Map<String, Object> condMap)
	{
		ApiResponse<MakeupCategory> apiResponse = new ApiResponse<MakeupCategory>();

		Map<String, MakeupCategory> data = new HashMap<String, MakeupCategory>();
		MakeupCategory cate = this.cateDao.findOne(condMap);
		data.put(ApiResponseEnum.NAME_ONE.getName(), cate);
		apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
		/* 将结果放到JSON中 */
		apiResponse.setDataOne(data);
		apiResponse.setDataOneJava(cate);
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
		return apiResponse;
	}
}
